package com.jiuair.cloud.data.revenue.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;

public class MD5Util {
    private static final String SALT = "";
    public static String encode(String password) {
        password = password + SALT;
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        char[] charArray = password.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString().toUpperCase();
    }

    private static final String HEX_NUMS_STR = "0123456789ABCDEF";
    private static final Integer SALT_LENGTH = 12;

    public MD5Util() {
    }

    public static byte[] hexStringToByte(String hex) {
        int len = hex.length() / 2;
        byte[] result = new byte[len];
        char[] hexChars = hex.toCharArray();

        for(int i = 0; i < len; ++i) {
            int pos = i * 2;
            result[i] = (byte)("0123456789ABCDEF".indexOf(hexChars[pos]) << 4 | "0123456789ABCDEF".indexOf(hexChars[pos + 1]));
        }

        return result;
    }

    public static String byteToHexString(byte[] b) {
        StringBuffer hexString = new StringBuffer();

        for(int i = 0; i < b.length; ++i) {
            String hex = Integer.toHexString(b[i] & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            hexString.append(hex.toUpperCase());
        }

        return hexString.toString();
    }

    public static boolean validPassword(String password, String passwordInDb) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] pwdInDb = hexStringToByte(passwordInDb);
        byte[] salt = new byte[SALT_LENGTH];
        System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(salt);
        md.update(password.getBytes("UTF-8"));
        byte[] digest = md.digest();
        byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH];
        System.arraycopy(pwdInDb, SALT_LENGTH, digestInDb, 0, digestInDb.length);


        return Arrays.equals(digest, digestInDb);
    }

    public static String codepass(String password,String passwordInDb) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] pwdInDb = hexStringToByte(passwordInDb);
        byte[] salt = new byte[SALT_LENGTH];
        System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(salt);
        md.update(password.getBytes("UTF-8"));
        byte[] digest = md.digest();
      //  byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH];
      //  System.arraycopy(pwdInDb, SALT_LENGTH, digestInDb, 0, digestInDb.length);
        String now = byteToHexString(digest);
     //   String s = byteToHexString(digestInDb);
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(salt);
        m.update(now.getBytes("UTF-8"));
        byte[] d = m.digest();

        return  byteToHexString(d);
}
    public static String code(String passwordInDb) throws NoSuchAlgorithmException, UnsupportedEncodingException {

        byte[] pwdInDb = hexStringToByte(passwordInDb);
        byte[] salt = new byte[SALT_LENGTH];
        System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
        byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH];
        System.arraycopy(pwdInDb, SALT_LENGTH, digestInDb, 0, digestInDb.length);
        String s = byteToHexString(digestInDb);
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(salt);
        m.update(s .getBytes("UTF-8"));
        byte[] d = m.digest();
        String s1 = byteToHexString(d);
        return s1;
    }


    public static String getEncryptedPwd(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
      /*  byte[] salt = new byte[SALT_LENGTH];
        MessageDigest md = MessageDigest.getInstance("MD5");
     //   byte[] pwdInDb = hexStringToByte(passwordInDb);
      //  System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
        md.update(salt);
        md.update(password.getBytes("UTF-8"));
        byte[] digest = md.digest();
      //  byte[] pwd = new byte[digest.length + SALT_LENGTH];
      //  System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);
        //System.arraycopy(digest, 0, pwd, SALT_LENGTH, digest.length);
       return byteToHexString(digest);*/
        byte[] pwd = null;
        SecureRandom random = new SecureRandom();
        byte[] salt = new byte[SALT_LENGTH];
        random.nextBytes(salt);
        MessageDigest md = null;
        md = MessageDigest.getInstance("MD5");
        md.update(salt);
        md.update(password.getBytes("UTF-8"));
        byte[] digest = md.digest();
        pwd = new byte[digest.length + SALT_LENGTH];
        System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);
        System.arraycopy(digest, 0, pwd, SALT_LENGTH, digest.length);
        return byteToHexString(digest);
    }

    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException {
       // System.out.println(encode("123456"));
        System.out.println(getEncryptedPwd("123456"));
        System.out.println("codepass:"+codepass("123456","C2868EE2B05BC8672293B6045B13CFE5032B168C988A7B5DC6027D6F"));
        String code = code("C2868EE2B05BC8672293B6045B13CFE5032B168C988A7B5DC6027D6F");
        System.out.println("zzzzzzzzzzzzzzzzzzzzzzz:"+validPassword("123456","C2868EE2B05BC8672293B6045B13CFE5032B168C988A7B5DC6027D6F"));
        System.out.println("code:"+code);
        System.out.println(encode("123456"));
      //  System.out.println("25711DC62F49B9DE2998726EE128538B");
//--------------------------------------------------------------------------------
        yuxiangtest("123456");

    }

    public static void yuxiangtest(String password)throws NoSuchAlgorithmException, UnsupportedEncodingException{
        String md5PassSalt=null;
        try {
            md5PassSalt= getEncryptedPwd_ori(password);
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("md5PassSalt: "+md5PassSalt);

        String result=validPassword_ori("123456","C2868EE2B05BC8672293B6045B13CFE5032B168C988A7B5DC6027D6F");
        System.out.println("result: "+result);



    }
    public static String getEncryptedPwd_ori(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        byte[] pwd = null;
        SecureRandom random = new SecureRandom();
        byte[] salt = new byte[SALT_LENGTH];
        random.nextBytes(salt);
        MessageDigest md = null;
        md = MessageDigest.getInstance("MD5");
        md.update(salt);
        md.update(password.getBytes("UTF-8"));
        byte[] digest = md.digest();
        pwd = new byte[digest.length + SALT_LENGTH];
        System.arraycopy(salt, 0, pwd, 0, SALT_LENGTH);
        System.arraycopy(digest, 0, pwd, SALT_LENGTH, digest.length);
        return byteToHexString(digest);
    }
    public static String validPassword_ori(String password,String passwordInDb) throws NoSuchAlgorithmException, UnsupportedEncodingException {

        byte[] pwdInDb = hexStringToByte(passwordInDb);
        byte[] salt = new byte[SALT_LENGTH];
        System.arraycopy(pwdInDb, 0, salt, 0, SALT_LENGTH);
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(salt);
        md.update(password.getBytes("UTF-8"));
        byte[] digest = md.digest();
        byte[] digestInDb = new byte[pwdInDb.length - SALT_LENGTH];
        System.arraycopy(pwdInDb, SALT_LENGTH, digestInDb, 0, digestInDb.length);
//        return Arrays.equals(digest, digestInDb);
        String s = byteToHexString(digestInDb);
        String a = byteToHexString(digest);
       // String result=new String(digestInDb,"ISO_8859_1");
        System.out.println(a);
        return s;
    }

}